var express = require("express")
var router = express.Router()
var QcloudSms = require("qcloudsms_js")
var connection = require("../../../db/sql")
var jwt = require("jsonwebtoken")
const bcrypt = require("bcryptjs")
var orderSql = require("./orderSql")

/*获取订单页数据值*/
router.get("/api/order/getorderdata", function (req, res, next) {
  let token = req.headers.token
  let tokenObj = jwt.decode(token)

  // 查询用户的id
  connection.query(orderSql.selectUserId(tokenObj), function (err, reslut) {
    if (err) {
      res.send({
        code: 201,
        msg: "查询错误",
        err,
      })
    }
    let id = reslut[0].user_id
    connection.query(orderSql.selectOrderData(id), function (errs, resluts) {
      if (errs) {
        res.send({
          msg: "查询错误",
          data: errs,
        })
      }

      // 处理返回的数据格式
      let shopArr = []
      let obj = {
        ordernumber_id: "",
        arr: [],
        total_price: "",
      }
      let values = resluts
      let orderNumber_Arrid = [
        ...new Set(
          values.map((item) => {
            return item.ordernumber_id.toString()
          })
        ),
      ]
      console.log(resluts)
      console.log(orderNumber_Arrid)

      orderNumber_Arrid.map((v) => {
        obj.ordernumber_id = v
        let totalprice = 0
        resluts.forEach((element) => {
          if (element.ordernumber_id == v) {
            obj.arr.push(element)
            totalprice =
              totalprice +
              parseInt(element.orderfrom_price) *
                parseInt(element.orderfrom_number)
          }
          obj.total_price = totalprice
        })
        shopArr.push(obj)

        // 为什么要清空喃
        obj = {
          ordernumber_id: "",
          arr: [],
          total_price: "",
        }
      })

      // console.log(orderNumber_Arrid)
      console.log("这是查询到的数据")
      console.log(...shopArr)

      res.send({
        status: 200,
        msg: "查询成功",
        data: shopArr.reverse(),
      })
    })
  })
})

/*获取待发货数据*/
router.get("/api/order/gettobeshippeddata", function (req, res, next) {
  let token = req.headers.token
  let tokenObj = jwt.decode(token)

  // 查询用户的id
  connection.query(orderSql.selectUserId(tokenObj), function (err, reslut) {
    if (err) {
      res.send({
        code: 201,
        msg: "查询错误",
        err,
      })
    }
    let id = reslut[0].user_id
    connection.query(
      orderSql.selectToBeShippedData(id),
      function (errs, resluts) {
        if (errs) {
          res.send({
            msg: "查询错误",
            data: errs,
          })
        }

        // 处理返回的数据格式
        let shopArr = []
        let obj = {
          ordernumber_id: "",
          arr: [],
          total_price: "",
        }
        let values = resluts
        let orderNumber_Arrid = [
          ...new Set(
            values.map((item) => {
              return item.ordernumber_id.toString()
            })
          ),
        ]
        console.log(resluts)
        console.log(orderNumber_Arrid)

        orderNumber_Arrid.map((v) => {
          obj.ordernumber_id = v
          let totalprice = 0
          resluts.forEach((element) => {
            if (element.ordernumber_id == v) {
              obj.arr.push(element)
              totalprice =
                totalprice +
                parseInt(element.orderfrom_price) *
                  parseInt(element.orderfrom_number)
            }
            obj.total_price = totalprice
          })
          shopArr.push(obj)

          // 为什么要清空喃
          obj = {
            ordernumber_id: "",
            arr: [],
            total_price: "",
          }
        })

        // console.log(orderNumber_Arrid)
        console.log("这是查询到的数据")
        console.log(...shopArr)

        res.send({
          status: 200,
          msg: "查询成功",
          data: shopArr.reverse(),
        })
      }
    )
  })
})

// 删除订单
router.post("/api/order/delorder", function (req, res, next) {
  let order_id = req.body.id
  console.log(order_id)
  // 查询订单号的id
  connection.query(orderSql.selectOrderId(order_id), function (err, reslut) {
    if (err) {
      res.send({
        code: 201,
        msg: "查询id出错",
        err,
      })
    }

    let id = reslut[0].id
    console.log(id)
    // 删除外键的表数据
    connection.query(orderSql.delForeignKeyData(id), function (e, r) {
      if (e) {
        res.send({
          code: 202,
          msg: "删除失败",
          e,
        })
      }

      // 删除主键的数据
      connection.query(orderSql.delKeyData(id), function (errs, resluts) {
        if (errs) {
          res.send({
            code: 203,
            msg: "删除失败",
            errs,
          })
        }

        res.send({
          code: 200,
          msg: "删除成功",
        })
      })
    })
  })
})

module.exports = router
